/**
 * 订单管理 交易订单
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>在拍商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：商品名称">
        <el-input size="small" v-model="formInline.goodsNameLike" placeholder="输入商品名称"></el-input>
      </el-form-item>

      <el-form-item label="选择店铺">
        <el-select v-model="formInline.shopId" placeholder="请选择店铺">
          <el-option  v-for="item in shopList"
                      :key="item.id"
                      :value="item.id"
                      :label="item.name"/>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="formInline.effFlg" placeholder="请选择状态">
          <el-option label="全部" value ></el-option>
          <el-option label="未启动" value=7></el-option>
          <el-option label="进行中" value=1></el-option>
          <el-option label="完成" value=2></el-option>
          <el-option label="暂停" value=0></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="success" icon="el-icon-s-open" @click="batchStart">批量恢复</el-button>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="danger" icon="el-icon-video-pause" @click="batchStop">批量暂停</el-button>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="warning" icon="el-icon-s-help" @click="refreshGoods">刷新商品信息</el-button>
      </el-form-item>

    </el-form>
    <div style="background:#c0ccda; color:#FF0000">
      1、关于顶价模式：快速模式以随机5到10分钟一手的机率进行顶价，一直到达到目标价为止。慢速模式按照计算得出的固定频率实行顶价，预计在截止时间前20分钟达到目标价。请慎重切换模式，尤其是快速模式切换为慢速模式时，因为模式切换导致的问题，工具不负任何责任。<br/>
      2、刷新商品信息：拉取第三方平台最新的商品和价格信息，拉取第三方平台信息需要时间，因此请在刷新后等待片刻再点"搜索"按钮查询最新信息。每次刷新间隔最少不得低于5分钟，否则平台不予受理。<br/>
    </div>
    <br/>
    <!--列表-->
    <el-table size="small" :data="listData" @selection-change="changeBox" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>

      <el-table-column sortable prop="platType" label="平台" width="100" show-overflow-tooltip :formatter = "converPlat">
      </el-table-column>
      <el-table-column label="图片" width="118">
        <template scope="scope">
          <img :src="scope.row.imageUrl" width="116" height="80"/>
        </template>
      </el-table-column>
      <el-table-column sortable prop="goodsName" label="名称" width="300" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="endTime" label="截止时间" width="150" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="startPrice" label="起拍价" width="85" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="currentPrice" label="当前价" width="85" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="minSalePrice" label="目标价" width="85" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="stepPrice" label="加一手价格" width="105" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="effFlg" label="状态" width="90" show-overflow-tooltip  :formatter = "convertStatus">
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.effFlg == 7" type="success" @click="setTargetPriceEdit(scope.$index, scope.row)">设定目标价</el-button>
          <el-button size="mini" v-if="scope.row.effFlg == 0" type="success" @click="setTargetPriceEdit(scope.$index, scope.row)">设定目标价</el-button>
          <el-button size="mini" type="danger"  @click="refreshPrice(scope.$index, scope.row)">最新价</el-button>
          <el-button size="mini" type="primary" @click="jysEdit(scope.$index, scope.row)">加一手</el-button>
          <el-button size="mini" v-if="scope.row.offerMode == 1" type="warning" @click="changeMode(scope.$index, scope.row)">慢速模式</el-button>
          <el-button size="mini" v-if="scope.row.offerMode == 0" type="warning" @click="changeMode(scope.$index, scope.row)">快速模式</el-button>
          <el-button size="mini" v-if="scope.row.effFlg == 1" type="danger" @click="changeStatus(scope.$index, scope.row)">暂停</el-button>
          <el-button size="mini" v-if="scope.row.effFlg == 0" type="success" @click="changeStatus(scope.$index, scope.row)">恢复</el-button>
          <el-button size="mini" type="info" @click="handleEdit(scope.$index, scope.row)">日志</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <el-dialog :title="jysTitle" :visible.sync="jysFormVisible" width="50%" @click="closeJysDialog('editForm')">
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="目标价">
              <el-input size="small" v-model="editForm.minSalePrice" auto-complete="off" disabled></el-input>
            </el-form-item>
            <el-form-item label="加一手金额">
              <el-input size="small" v-model="editForm.stepPrice" auto-complete="off"  disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeJysDialog("editForm")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitJysForm('editForm')">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="targetPriceTitle" :visible.sync="targetPriceFormVisible" width="50%" @click="closeTargetPriceDialog('editForm')">
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="目标价">
              <el-input size="small" v-model="editForm.minSalePrice"></el-input>
            </el-form-item>

            <div style="color:#F00">设置目标价后，将直接启动顶价</div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeTargetPriceDialog("editForm")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitTargetForm('editForm')">确认</el-button>
      </div>
    </el-dialog>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%">
      <el-table size="small" :data="offerHisData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
        <el-table-column align="center" type="index" width="60">
        </el-table-column>

        <el-table-column sortable prop="offerType" label="出价方式" width="100" show-overflow-tooltip :formatter = "converOfferType">
        </el-table-column>

        <el-table-column sortable prop="price" label="出价金额" width="120" show-overflow-tooltip>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { goodsList, jysPrice, offerHisList,changeStatus
  ,batchChangeStatus,changeTargetPrice,changeMode,refreshGoods,refreshPrice } from '../../api/GoodsMG'

import {  allShops } from '../../api/shopMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      jysFormVisible: false, //控制编辑页面显示与隐藏
      targetPriceFormVisible :false,
      title: '顶价日志',
      jysTitle:'加一手',
      targetPriceTitle:'设置目标价',
      editForm: {
        id: '',
        price: null,
        offerType: null
      },
      formInline: {
        pageNo: 1,
        pageSize: 10,
        goodsNameLike: null,
        effFlg:null,
        shopId:null
      },
      shopList:[

      ],

      userparm: [], //搜索权限
      listData: [], //用户数据
      offerHisData: [], //用户数据
      selectedList:[],
      selectedItemList:[],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.initShopList();
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    converPlat(row, column){
      if(!row.platType){
        return "未知"
      }

      if(row.platType ==1 ){
        return "玩物得志"
      }
      return "其他"
    },
    converOfferType(row, column){
      if(!row.offerType){
        return "未知"
      }

      if(row.offerType ==1 ){
        return "手动出价"
      }
      return "系统出价"
    },
    convertStatus(row, column){
      if(row.effFlg ==7){
        return "未启动"
      }
      if(row.effFlg ==1){
        return "进行中"
      }

      if(row.effFlg ==2){
        return "完成"
      }
      if(row.effFlg ==0){
        return "暂停"
      }
      return "其他"
    },

    initShopList(){
      allShops()
        .then(res => {
          debugger
          if (res.code != '0000') {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.shopList = res.body
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('店铺加载失败，请稍后再试！')
        })
    },
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
      parameter.queryTimeFlg=1;

      // if (parameter.effFlg != undefined && parameter.effFlg != 'undefined'){
      //   parameter.effFlg = parseInt(parameter.effFlg)
      // }
        // 模拟数据开始
      // let res = {
      //   code: 0,
      //   msg: null,
      //   count: 23,
      //   data: [
      //     {
      //       overTime: 1526380193000,
      //       startPrice: 109,
      //       currentPrice: 209,
      //       targetPrice:1099,
      //       goodsName:"浪琴典藏表",
      //       imageUrl:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1441706262,2991361536&fm=11&gp=0.jpg",
      //       id:1011
      //     },{
      //       overTime: 1526380193000,
      //       startPrice: 109,
      //       currentPrice: 209,
      //       targetPrice:1099,
      //       goodsName:"浪琴限量款",
      //       imageUrl:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1441706262,2991361536&fm=11&gp=0.jpg",
      //       id:1012
      //     }
      //   ]
      // }
      // this.loading = false
      // this.listData = res.data
      // this.pageparm.currentPage = this.formInline.page
      // this.pageparm.pageSize = this.formInline.limit
      // this.pageparm.total = res.count
      // 模拟数据结束

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      goodsList(parameter)
         .then(res => {
           this.loading = false
           if (res.code != '0000') {
             this.$message({
               type: 'info',
               message: res.msg
             })
           } else {
             this.listData = res.body.list
             // 分页赋值
             this.pageparm.currentPage = this.formInline.pageNo
             this.pageparm.pageSize = this.formInline.pageSize
             this.pageparm.total = res.body.total
           }
         })
         .catch(err => {
           this.loading = false
           this.$message.error('商品加载失败，请稍后再试！')
         })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNo = parm.currentPage
      this.formInline.pageSize = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },

    refreshPrice(index, row){
      this.loading = true
      refreshPrice(row).then(
        res=>{
          this.loading = false
          if (res.code== '0000') {
            this.$message({
              type: 'success',
              message: '最新价格刷新成功'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        }
      ).catch(err => {
        this.editFormVisible = false
        this.loading = false
        this.$message.error('查询失败，请稍后再试！')
      })
    },

    batchStop(index, row){
      if(this.selectedList.length <=0){
        this.$message.error('请选中要暂停的记录！')
        return
      }

      for(var x=0;x<this.selectedItemList.length;x++){
        var f=this.selectedItemList[x].effFlg
        if(f==0 || f == 2){
          this.$message.error(this.selectedItemList[x].goodsName+' 不需要暂停！')
          return
        }
      }

      this.loading = true
      let p ={}
      p.effFlg =0
      p.ids =this.selectedList
      batchChangeStatus(p).then(
        res=>{
          this.loading = false
          if (res.code== '0000') {
            this.$message({
              type: 'success',
              message: '请求提交完成，请稍后刷新查看结果'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        }
      ).catch(err => {
        this.editFormVisible = false
        this.loading = false
        this.$message.error('查询失败，请稍后再试！')
      })
    },

    batchStart(index, row){
      if(this.selectedList.length <=0){
        this.$message.error('请选中要启动的记录！')
        return
      }

      for(var x=0;x<this.selectedItemList.length;x++){
        var f=this.selectedItemList[x].effFlg
        if(f!=0){
          this.$message.error(this.selectedItemList[x].goodsName +' 不需要恢复！')
          return
        }
      }

      this.loading = true
      let p ={}
      p.effFlg =1
      p.ids =this.selectedList
      batchChangeStatus(p).then(
        res=>{
          this.loading = false
          if (res.code== '0000') {
            this.$message({
              type: 'success',
              message: '请求提交完成，请稍后刷新查看结果'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        }
      ).catch(err => {
        this.editFormVisible = false
        this.loading = false
        this.$message.error('查询失败，请稍后再试！')
      })
    },

    changeBox(val) {
      this.selectedList = []
      this.selectedItemList =[]
      val.forEach((item) => {
        this.selectedList.push(item.id)
        this.selectedItemList.push(item)
      })
    },

    refreshGoods(){
      this.loading = true
      refreshGoods().then(
        res=>{
          this.loading = false
          if (res.code== '0000') {
            this.$message({
              type: 'success',
              message: '刷新请求受理成功，请稍后查看最新商品信息'
            })
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        }
      ).catch(err => {
        this.editFormVisible = false
        this.loading = false
        this.$message.error('查询失败，请稍后再试！')
      })
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      //加载数据
      offerHisList(row).then(res => {
        this.editFormVisible = false
        this.loading = false
        if (res.code== '0000') {
           this.offerHisData=res.body
          this.editFormVisible = true
        } else {
          this.$message({
            type: 'info',
            message: res.msg
          })
        }
      })
        .catch(err => {
          this.editFormVisible = false
          this.loading = false
          this.$message.error('查询失败，请稍后再试！')
        })
    },

    setTargetPriceEdit: function(index, row) {
      this.targetPriceFormVisible = true
      this.editForm = row
    },

    jysEdit: function(index, row) {
      this.jysFormVisible = true
      this.editForm = row
    },

    submitJysForm(editData){
      jysPrice(this.editForm)
        .then(res => {
          this.jysFormVisible = false
          this.loading = false
          if (res.code== '0000') {
            this.getdata(this.formInline)
            this.$message({
              type: 'success',
              message: '加价成功！'
            })
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.jysFormVisible = false
          this.loading = false
          this.$message.error('加价失败，请稍后再试！')
        })
    },

    submitTargetForm(editData){
      if(!this.editForm.minSalePrice){
        this.loading = false
        this.$message.error('请输入目标价！')
        return false;
      }

      changeTargetPrice(this.editForm)
        .then(res => {
          this.targetPriceFormVisible = false
          this.loading = false
          if (res.code== '0000') {
            this.getdata(this.formInline)
            this.$message({
              type: 'success',
              message: '设置成功！'
            })
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.targetPriceFormVisible = false
          this.loading = false
          this.$message.error('设置失败，请稍后再试！')
        })
    },

    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          ConfigSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('支付配置信息保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },

    changeMode(index, row) {

      let cmsg = '确定要切换为慢速模式吗?'
      if(row.offerMode==0){
        cmsg = '确定要切换为快速模式吗?'
      }

      this.$confirm(cmsg, '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let par={}
          par.id = row.id;

          if(row.offerMode==1){
            par.offerMode =0;
          }
          if(row.offerMode==0){
            par.offerMode =1;
          }
          changeMode(par)
            .then(res => {
              if (res.code=='0000') {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('切换模式失败，请稍后再试！')
            })
        })
        .catch((err) => {
          console.error(err)
          debugger
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 删除公司
    changeStatus(index, row) {
      if(row.effFlg==2){
        this.$message({
          type: 'info',
          message: '不允许操作完成状态的数据'
        })
        return;
      }

      let cmsg = '确定要暂停吗?'
      if(row.effFlg==0){
        cmsg = '确定要恢复吗?'
      }

      this.$confirm(cmsg, '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let par={}
          par.id = row.id;

          if(row.effFlg==1){
            par.effFlg =0;
          }
          if(row.effFlg==0){
            par.effFlg =1;
          }
          changeStatus(par)
            .then(res => {
              if (res.code=='0000') {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('信息删除失败，请稍后再试！')
            })
        })
        .catch((err) => {
          console.error(err)
          debugger
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog(formName) {
      this.editFormVisible = false
      this.$refs[formName].resetFields()
    },

    closeJysDialog(formName) {
      this.jysFormVisible = false
    },
    closeTargetPriceDialog(formName) {
      this.targetPriceFormVisible = false
    }

  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>




